package Servlet;
import Utils.DbUtil;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/ToUpdateServlet")
public class ToUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String[] row = req.getParameterValues("row");
        String tablename = req.getParameter("tablename");
        Map<String, String> rows = new HashMap<String, String>();
        List<String> columnNames = DbUtil.getColumnCount(req,tablename);
        if (row != null && columnNames != null) {
            for (int i = 0; i < columnNames.size(); i++) {
                rows.put(columnNames.get(i), row[i]);
            }
        }
        req.setAttribute("rows", rows);
        req.setAttribute("tablename", tablename);
        RequestDispatcher dispatcher = req.getRequestDispatcher("update.jsp");
        dispatcher.forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }
}
